﻿using AutoMapper;
using HandyControl.Controls;
using HandyControl.Data;
using MaterialTypeRecognition.Shell.Geometries;
using MaterialTypeRecognition.Shell.SaveInfos;
using MaterialTypeRecognition.Shell.SaveInfoService;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Windows.Input;
using System.Windows.Media;

namespace MaterialTypeRecognition.Shell.ViewModels
{
    public class MesConfigurationWindowViewModel : BindableBase
    {
        private const string GrowlToken = "MesConfigurationToken";

        public MesConfigurationWindowViewModel(ISaveInfoService saveInfoService, IMapper mapper)
        {
            var saveInfo = saveInfoService.Read<MesConfigurationSaveInfo>();
            mapper.Map(saveInfo, this);
            SaveCommand = new DelegateCommand<object>(async (_) =>
            {
                bool isSuccess = true;

                try
                {
                    await saveInfoService.WriteAsync(mapper.Map<MesConfigurationSaveInfo>(this));
                }
                catch (Exception)
                {
                    isSuccess = false;
                }

                var info = new GrowlInfo()
                {
                    ShowDateTime = false,
                    ShowCloseButton = true,
                    WaitTime = 1,
                    Token = GrowlToken
                };

                if (isSuccess)
                {
                    info.Icon = Icons.SuccessIcon;
                    info.IconBrush = Brushes.Green;
                    info.Message = "保存成功!";
                    Growl.Info(info);
                }
                else
                {
                    info.Icon = Icons.ErrorIcon;
                    info.IconBrush = Brushes.Red;
                    info.Message = "保存失败!";
                    Growl.Warning(info);
                }
            });
        }

        public string? Url { get; set; }

        public string? UserName { get; set; }

        public string? Password { get; set; }

        public string? Site { get; set; }

        public string? Resource { get; set; }

        public string? ModeProcessSfc { get; set; }

        public string? Operation { get; set; }

        public string? Activity { get; set; }

        public string? OperationRevision { get; set; }

        public string? User { get; set; }

        public string? ProcessLot { get; set; }

        public ICommand SaveCommand { get; set; }
    }
}
